package com.jc.subject.domin.handler.subject;

import com.jc.subject.common.enums.IsDeletedFlagEnum;
import com.jc.subject.common.enums.SubjectInfoTypeEnum;
import com.jc.subject.domin.convert.SubjectMultipleConverter;
import com.jc.subject.domin.convert.SubjectRadioConverter;
import com.jc.subject.domin.entity.SubjectAnswerBO;
import com.jc.subject.domin.entity.SubjectInfoBO;
import com.jc.subject.domin.entity.SubjectOptionBO;
import com.jc.subject.infra.basic.entity.SubjectMultiple;
import com.jc.subject.infra.basic.entity.SubjectRadio;
import com.jc.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description: 多选题目的策略类
 * @Author: ASL_ly
 * @Package: com.jc.subject.domin.handler.subject
 * @Project: jc-club
 * @Date: 2024/10/23  20:38
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectMultiple> subjectMultipleList = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option ->{
            SubjectMultiple subjectMultiple = SubjectMultipleConverter.INSTANCE.subjectAnswerBoToMultiple(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
            subjectMultipleList.add(subjectMultiple);
        });

        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectMultiple subjectMultiple= SubjectMultiple.builder()
                .subjectId(subjectId)
                .isDeleted(IsDeletedFlagEnum.UN_DELETED.code)
                .build();
        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOList = SubjectMultipleConverter
                .INSTANCE.subjectMultipleToAnswerBOList(subjectMultipleList);
        SubjectOptionBO subjectOptionBO =new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
